#include <iostream>

#include<memory>
using namespace std;

//example 1: shallow copy by implicit copy constructor causes runtime error
class Array {
public:
	//int *ptr;
	
	//1: changed to auto_ptr
	std::unique_ptr<int> ptr;	

	int size;
 	explicit Array(int s): size(s)  {
		int *i = new int[size];
		ptr = unique_ptr<int>(i);
	}

	~Array() {
		//if(ptr) delete [] ptr;
		//ptr = 0;	
		cout << "Array of size " << size <<" is destructed\n";
	}

	/*Array(const Array& copy_from_me) : size(copy_from_me.size) {
		cout << "Array of size " << size << " Copy constructor called\n";
	};*/
	
};	

Array retArray4() {
	
	Array arr4(4);
	arr4.ptr.get()[0] = 4;
	return arr4;
}

int main (int argc, char* argv[]) {
	//error case 1(possible, but not displays here:
	//Array arr1 = Array(1);
	//arr1.ptr.get()[0] = 0;

	
	//error case 2:
	const Array arr2(2);
	{
		Array arr2_copy = arr2;
	}
	if(arr2.ptr.get() == NULL) cout << "arr2 ptr is NULL\n";
	else arr2.ptr.get()[0] = 1;//segm fault
	
	//error case 2.1:
	{
	//	Array arr2_copy(1);
	//	arr2_copy = arr2;
	//	arr2_copy.ptr.get()[0] = 1;
	}
	//arr2.ptr[0] = 1;//no error

	
	//error case 3(possible, but not displayed here):
	//Array arr4 = retArray4();
	//arr4.ptr.get()[3] = 10;

}
